﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LightWeb.Respository;
using LightWeb.DomainModel;
using NHibernate.Criterion;
using NHibernate;
using LightWeb.TransferModel;
using FlyearAssist.Hibernating.DataModel;

namespace LightWeb.Blog
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadPageInitData();
            }
        }

        private void LoadPageInitData()
        {
            LoadArticalCategoryByDate();
            LoadTop10ArticalByPostdDate();
            LoadTop10CommentByPostDate();
        }

        private void LoadTop10CommentByPostDate()
        {
            List<TmArticalComment> summary = new List<TmArticalComment>();

            using (OurTransactionScope ts = new OurTransactionScope())
            {
                var context = DataContext.Current;
                var list = context.Read<ArticalComment>(1, 10, null, new List<NOrder> { new NOrder { PropertyName = "Date", OrderType = OrderDirection.DESC } }).ModelList;
                foreach (ArticalComment item in list)
                {
                    summary.Add(ModelConvert.FromDomainModel(item));
                }
            }
            this.repTop10CommentByPostDate.DataSource = summary;
            this.repTop10CommentByPostDate.DataBind();
        }

        private void LoadTop10ArticalByPostdDate()
        {
            List<TmArcitalSummary> summary = new List<TmArcitalSummary>();

            using (OurTransactionScope ts = new OurTransactionScope())
            {
                var context = DataContext.Current;
                var list = context.Read<ArticalInfo>(1, 10, null, new List<NOrder> { new NOrder { PropertyName = "PostDate", OrderType = OrderDirection.DESC } }).ModelList;
                foreach (ArticalInfo item in list)
                {
                    summary.Add(ModelConvert.FromDomainModel(item));
                }
            }
            this.repTop10ArticalByPostDate.DataSource = summary;
            this.repTop10ArticalByPostDate.DataBind();
        }

        private void LoadArticalCategoryByDate()
        {
            var listDate = new List<KeyValuePair<string, string>>();
            using (OurTransactionScope ts = new OurTransactionScope())
            {
                var context = DataContext.Current;
                var val = context.Session.CreateCriteria<ArticalInfo>()
                .SetProjection(Projections.ProjectionList()
                .Add(Projections.RowCount(), "CountByPostDate")
                    //.Add(Projections.Avg("Weight"), "avgWeight")
                    //.Add(Projections.Max("Weight"), "maxWeight")
                .Add(Projections.GroupProperty(Projections.SqlFunction("year", NHibernateUtil.DateTime, Projections.Property("PostDate"))))
                .Add(Projections.GroupProperty(Projections.SqlFunction("month", NHibernateUtil.DateTime, Projections.Property("PostDate"))))
                )
                    //.AddOrder(Order.Desc("catCountByColor"))
                    //.AddOrder(Order.Desc("avgWeight"))
                .List<object[]>();

                foreach (var item in val)
                {
                    //listDate.Add(string.Format("{0}-{1} ({2})", item[1], item[2], item[0]));
                    listDate.Add(new KeyValuePair<string, string>(string.Format("{0}-{1}", item[1], item[2]), item[0].ToString()));
                }
            }
            this.repArticalByDate.DataSource = listDate;
            this.repArticalByDate.DataBind();
        }
    }
}
